<script setup>
import { Edit, Delete } from '@element-plus/icons-vue'
import { onMounted, ref } from 'vue'
import { cateListServer, deleteCateServer } from '@/api/article.js'
import PageContainer from '@/components/PageContainer.vue'
import ChannelEdit from '@/views/article/component/ChannelEdit.vue'

const channelList = ref([])
const loading = ref(false)
onMounted(() => {
  cateList()
})
// 获取文章分类
const cateList = async () => {
  loading.value = true
  const res = await cateListServer()
  channelList.value = res.data
  loading.value = false
}

// 添加文章分类
const dialog = ref(null)
const onAddChannel = () => {
  dialog.value.open({})
}

// 编辑
const onEditChannel = row => {
  dialog.value.open(row)
}
// 删除
const onDelChannel = async row => {
  await ElMessageBox.confirm('确定删除该分类吗?', '温馨提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning'
  })
  await deleteCateServer(row.id)
  ElMessage.success('删除成功')
  await cateList()
}
</script>

<template>
  <!--  组件无需引用因为有安装unplugin-vue-components插件-->
  <page-container title="文章分类">
    <template #extra>
      <el-button type="primary" @click="onAddChannel">添加分类</el-button>
    </template>
    <!--    添加分类弹层-->
    <channel-edit ref="dialog" @success="cateList"></channel-edit>
    <!--    表格-->
    <el-table v-loading="loading" :data="channelList" style="width: 100%">
      <el-table-column label="序号" width="100" type="index"> </el-table-column>
      <el-table-column label="分类名称" prop="cate_name"></el-table-column>
      <el-table-column label="分类别名" prop="cate_alias"></el-table-column>
      <el-table-column label="操作" width="100">
        <template #default="{ row }">
          <el-button
            :icon="Edit"
            circle
            plain
            type="primary"
            @click="onEditChannel(row)"
          ></el-button>
          <el-button
            :icon="Delete"
            circle
            plain
            type="danger"
            @click="onDelChannel(row)"
          ></el-button>
        </template>
      </el-table-column>
      <template #empty>
        <el-empty description="没有数据" />
      </template>
    </el-table>
  </page-container>
</template>

<style scoped></style>
